package com.awesome.designpatterndemo.proxy.proxy4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.awesome.designpatterndemo.R;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Proxy4Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proxy4);
        // 方法二，代码全部在一处.只需要真实类。
        ILawsuit xiaomin2 = new XiaoMin();
        ILawsuit lawsuit2 = (ILawsuit) Proxy.newProxyInstance(xiaomin2.getClass().getClassLoader(), new Class[]{ILawsuit.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 调用被代理类对象的方法
                method.invoke(xiaomin2, args);
                return null;
            }
        });
        lawsuit2.submit();
        lawsuit2.burden();
        lawsuit2.defend();
        lawsuit2.finish();
    }
}